/*
 * Copyright 2019 Zhenjie Yan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.chushizhang;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.mingmen.canting.R;
import com.mingmen.canting.adapter.BohuiListAdapter;
import com.mingmen.canting.adapter.MyLabelAdapter;
import com.mingmen.canting.definerecycleview.ExpandableAdapter;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.BeiZhuModel;
import com.mingmen.canting.model.Group;
import com.mingmen.canting.model.GroupMember;
import com.mingmen.canting.model.ZhenBanShenHe;
import com.mingmen.canting.util.GsonUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Zhenjie Yan on 1/30/19.
 */
public class DingShenAdapter extends ExpandableAdapter<ExpandableAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<Group> mGroupList;
    Context context;
    ButtomDialogView bottomSheetDialog;
    private int checkedPosition = -1;

    public interface OnItemClickListener {
        //        void onItemClickListener(int pos, DaishenheListAdapter.ViewName viewName, List<MyLiveList> myLiveList);
        void onItemClickListener(View view, int pos, List<Group> groupList);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public DingShenAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
        createDialog();
    }

    public void setGroupList(List<Group> groupList) {
        this.mGroupList = groupList;
    }

    @Override
    public int parentItemCount() {
        return mGroupList == null ? 0 : mGroupList.size();
    }

    @Override
    public int childItemCount(int parentPosition) {
        List<GroupMember> memberList = mGroupList.get(parentPosition).getMemberList();
        return memberList == null ? 0 : memberList.size();
    }

    @Override
    public ViewHolder createParentHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_expand_parent, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_test, root, false);

        return new ParentHolder(view, this);
    }

    @Override
    public ViewHolder createChildHolder(@NonNull ViewGroup root, int viewType) {
//        View view = mInflater.inflate(R.layout.item_chushi_shenhe_child, root, false);
        View view = mInflater.inflate(R.layout.item_expand_test, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_child, root, false);
        return new ChildHolder(view, this);
    }

    @Override
    public void bindParentHolder(@NonNull ViewHolder holder, int position) {
//        ((ParentHolder) holder).setData(mGroupList.get(position));
        Group data = mGroupList.get(position);
        ParentHolder parentHolder = (ParentHolder) holder;
        parentHolder.mTvTitle.setText(data.getName());
        Log.d("ISEXPAndedisexpanded", data.isExpanded() + "");
//        layout_parent
        parentHolder.mIvStatus.setSelected(data.isExpanded());
        if (data.isExpanded() == true) {
            parentHolder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_item2));
        } else {
            parentHolder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem1));
        }

    }

    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    @Override
    public void bindChildHolder(@NonNull ViewHolder holder, int parentPosition, int position) {
//        ((ChildHolder)holder).setData(mGroupList.get(parentPosition).getMemberList().get(position));
        ChildHolder myholder = (ChildHolder) holder;
//        myholder.setData(mGroupList.get(parentPosition).getMemberList().get(position));
        mGroupList.get(parentPosition).isExpanded();
        int childsize = mGroupList.get(parentPosition).getMemberList().size();
//        shape_chushi_dingdan_ziitem3
        if (position == childsize-1) {
            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem2));
        } else {
            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        }
//        myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        GroupMember data = mGroupList.get(parentPosition).getMemberList().get(position);
        myholder.mTvTitle.setText(data.getName());
        myholder.layout_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, position + "", Toast.LENGTH_SHORT).show();
                bottomSheetDialog.show();
            }
        });

    }

    static class ParentHolder extends ViewHolder {

        TextView mTvTitle;
        ImageView mIvStatus;
        RelativeLayout layout_parent;

        public ParentHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            mIvStatus = itemView.findViewById(R.id.iv_status);
            layout_parent = itemView.findViewById(R.id.layout_parent);
        }

        public void setData(Group data) {
            mTvTitle.setText(data.getName());
            mIvStatus.setSelected(data.isExpanded());
        }
    }

    static class ChildHolder extends ViewHolder {

        TextView mTvTitle;
        LinearLayout layout_id;
        LinearLayout layout_item;

        public ChildHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            layout_id = itemView.findViewById(R.id.layout_id);
            layout_item = itemView.findViewById(R.id.layout_item);
        }

        public void setData(GroupMember data) {
            mTvTitle.setText(data.getName());
        }
    }

    public void createDialog() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);

        View view1 = layoutInflater.inflate(R.layout.dialog_chushi_bottom, null);

        bottomSheetDialog = new ButtomDialogView(context, view1, true, true);
        bottomSheetDialog.setContentView(R.layout.toast_dialog);

        RecyclerView recycle_biaoqian = view1.findViewById(R.id.recycle_biaoqian);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        recycle_biaoqian.setLayoutManager(gridLayoutManager);

        ImageView img_close = view1.findViewById(R.id.img_close);
        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.dismiss();
            }
        });

        EditText verify_et = view1.findViewById(R.id.verify_et);

        TextView text_submit = view1.findViewById(R.id.text_submit);
        text_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

}